<?php
class Article_Model extends CI_Model {
	/**
	 * 构造函数
	 *
	 * @access public
	 */
	function __construct() {
		parent::__construct();
	}
	/**
	 * 添加文章
	 *
	 * @access public
	 * @param array 预格式化的文章信息，通常从$this->input->post()中提取
	 * @return int 返回状态码，成功返回0，添加失败返回-1
	 */
	function add_art($post) {
		$query = $this->db->insert('wb_article', $post);
		if($query == TRUE) return 0;
		else return -1;
	}
	
	function get_art_list($condition = NULL) {
		$query = $this->db->query('SELECT art_id, type, title, time FROM wb_article');
		if($query->num_rows() > 0) {
			$item = array();
			foreach($query->result_array() as $row) {
				$item [] = $row;
			}
			return $item;
		}
		else return array();
	}

	function get_latest_art_list($condition = NULL) {

	}

	function get_art_count($condition = NULL) {
		$query = $this->db->query('SELECT COUNT(art_id) as count FROM wb_article');
		if($query->num_rows() > 0) {
			$res = $query->row_array();
			return $res['count'];
		}
		else return -1;
	}
	/**
	 * 修改文章
	 *
	 * @access public
	 * @param int 待修改文章ID
	 * @param array 预格式化的文章信息，通常从$this->input->post()中提取
	 * @return int 返回状态码，成功返回0，修改失败返回-1
	 */
	function edit_art($id, $post) {
		$query = $this->db->update('wb_article', $post, array('art_id' => $id));
		if($query == TRUE) return 0;
		else return -1;
	}
	/**
	 * 删除文章
	 * 
	 * @access public
	 * @param int 待删除文章ID
	 * @return int 返回状态码，成功返回0，删除失败返回-1
	 */
	function delete_art($id) {
		$query = $this->db->query('DELETE FROM wb_article where art_id = '.$id);
		if($query == TRUE) return 0;
		else return -1;
	}

	/**
	 * 获得完整文章信息
	 *
	 * @access public
	 * @param int 文章ID
	 * @return array 文章信息数组，查询为空返回空数组
	 */
	function get_full_art($id) {
		$query = $this->db->query('SELECT * FROM wb_article WHERE art_id = '. $id);
		if($query->num_rows() > 0 ){
			return $query->row_array();
		}
		else return array();
	}
}
/* End of file article_model.php */